package fr.mathis.tbbtmoments;

import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;
import com.actionbarsherlock.app.SherlockActivity;

import fr.mathis.tbbtmoments.dummy.DummyContent.DummyItem;

public class LoadActivity extends SherlockActivity{


	ProgressBar pb;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.loadactivity);
		pb = (ProgressBar)findViewById(R.id.progressBar1);


		new LoadPage().execute();
	}

	public String GetTwoDigitNumber(int i)
	{
		if(i<10)
			return "0"+i;
		else return i+"";

	}

	private class LoadPage extends AsyncTask <Void, String, Void>
	{
		Typeface tf;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			tf = Typeface.createFromAsset(getApplication().getAssets(),"fonts/Roboto-BoldCondensed.ttf");
			pb.setIndeterminate(false);
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			try{
				if(StateOlder.getInstance().getItems().size()==0)
				{
					Resources res = getResources();
					InputStream in_s = res.openRawResource(R.raw.tbbt);
	
					DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
					DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
					Document doc = docBuilder.parse(in_s);
	
					NodeList seasons = doc.getElementsByTagName("season");	
					
					int count = 0;
					
					int max = doc.getElementsByTagName("episode").getLength();
					
					for(int s = 0 ; s < seasons.getLength() ; s++)
					{
						Element currentSeason = (Element)seasons.item(s);
						String season = currentSeason.getAttribute("name");
	
						NodeList episodes =  currentSeason.getElementsByTagName("episode");
						for(int e = 0 ; e < episodes.getLength() ; e++)
						{
							Element currentEpisode = (Element)episodes.item(e);
							String episode = currentEpisode.getAttribute("name");
							String content = currentEpisode.getTextContent();
	
							String part = "";
							String[] vals = {"Sheldon","Alex","Leonard","Leslie","Raj","Howard","Kurt","Missy","Gablehauser","Barry","Beverly","Stuart","Mrs. Cooper","Bernadette","Penny","Abby","Martha","Stan Lee","Wil Wheaton","Elizabeth","Amy","Priya","Mrs. Wolowitz","SIRI","Stephen"};
	
							for(String v : vals)
							{
								if(content.contains(v+":"))
								{
									part += ", "+v;
								}
							}
	
							if(part.length()>2)
							{
								part = part.substring(2);
							}
							StateOlder.getInstance().addItem(new DummyItem((s+"")+(e+"")+"", content,season,(s+1)+"",episode,GetTwoDigitNumber(e+1),part,tf));
							count++;
							publishProgress((count*100/(max))+"");
						}
					}
				}
			}
			catch(Exception e)
			{
				Log.d("ERROR","Message : "+e.getMessage());
			}

			return null;
		}

		@Override
		protected void onProgressUpdate(String... values){
			super.onProgressUpdate(values);
			
			
			pb.setProgress(Integer.parseInt(values[0]));
		}

		@Override
		protected void onPostExecute(Void result) {
			pb.setProgress(100);

			Intent intent = new Intent(LoadActivity.this,QuoteListActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.fadein, R.anim.fadeout);
			finish();
		}
	}

}
